package com.mrent.web.controller.front;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.mrent.entity.CartGoods;
import com.mrent.entity.GoodsOrderInfo;
import com.mrent.entity.ManagerPayment;
import com.mrent.entity.ManagerShipping;
import com.mrent.entity.Member;
import com.mrent.entity.OrderGoods;
import com.mrent.service.CartGoodsService;
import com.mrent.service.GoodsOrderService;
import com.mrent.service.GoodsService;
import com.mrent.service.ManagerPaymentService;
import com.mrent.service.ManagerShippingService;
import com.mrent.service.OrderGoodsService;
import com.mrent.service.Page;
import com.mrent.util.Constance;
import com.mrent.util.Utils;

@Controller
public class GoodsOrderController {
	
	@Autowired
	private ManagerPaymentService managerPaymentService;
	@Autowired
	private ManagerShippingService managerShippingService;
	@Autowired
	private GoodsService goodsService;
	@Autowired
	private GoodsOrderService goodsOrderService;
	@Autowired
	private OrderGoodsService orderGoodsService;
	
	@Autowired
	private CartGoodsService cartGoodsService;
	
	/**
	 * 商品下定单.
	 * 
	 * @return
	 */
	@RequestMapping("/goods/order/add")
	public String add(HttpSession session,ModelMap model){
		
		Object member = session.getAttribute(Constance.SESSION_MEMBER_NAME);
		
		if(member == null){
			model.addAttribute("msg", "您尚未登录，请您先登录");
			return "front/goodsorder/Error";
		}
		String sessionId = session.getId();
		List<CartGoods> list = cartGoodsService.findBySession(sessionId);
		if(list != null && list.size() >0){
			//购物车信息.
			BigDecimal totalPrice = new BigDecimal(0);
			for(CartGoods cartGoods:list){
				BigDecimal goodsAmout = new BigDecimal(cartGoods.getGoodsAmount());
				BigDecimal goodsPrice = cartGoods.getGoodsPrice();
				BigDecimal goodsTotalPrice = goodsPrice.multiply(goodsAmout);
				totalPrice = totalPrice.add(goodsTotalPrice);
			}
			model.addAttribute("cart", list);
			model.addAttribute("cartPrice", totalPrice);
			
			String manager = null;
			
			Page page = new Page();
			
			for(CartGoods cg:list){
				manager = goodsService.getGoods(cg.getGoodsNo()).getManager();
				break;
			}
			//店主.
			model.addAttribute("manager", manager);
			//配送方式 .
			List<ManagerShipping> shippingList = managerShippingService.listShipping(manager, page);
			model.addAttribute("shippingList", shippingList);
			
			//支付方式.
			List<ManagerPayment> paymentList = managerPaymentService.listPayment(manager, page);
			model.addAttribute("paymentList", paymentList);
			
			return "front/goodsorder/add";
		}else{
			model.addAttribute("msg", "您的购物车中没有商品哦");
			return "front/goodsorder/Error";
		}
		
	}
	
	/**
	 * 订单保存
	 * @return
	 */
	@RequestMapping("/goods/order/save")
	public String save(GoodsOrderInfo goodsOrder,HttpSession session){
		//保存订单信息.
		goodsOrder.setOrderSn(Utils.generDataNo());
		Member member = (Member)session.getAttribute(Constance.SESSION_MEMBER_NAME);
		goodsOrder.setUserName(member.getMemberName());
		goodsOrder.setOrderStatus(new Integer(1));//1，已确认
		goodsOrder.setShippingStatus(new Integer(0));//0，未发货
		goodsOrder.setPayStatus(new Integer(0));//0，未付款
		String shippingName = managerShippingService.getShipping(goodsOrder.getShoperShippingId()).getShippingName();
		goodsOrder.setShoperShippingName(shippingName);
		String paymentName = managerPaymentService.getPayment(goodsOrder.getShoperPayId()).getPayName();
		goodsOrder.setShoperPayName(paymentName);
		goodsOrder.setInsureFee(new BigDecimal(0));//保价费用.没用设为0
		goodsOrder.setPayFee(new BigDecimal(0));//支付费用，暂时设为0
		goodsOrder.setDiscount(new BigDecimal(0));//折扣金额，暂时设为0
		BigDecimal orderAmount = goodsOrder.getGoodsAmount().add(goodsOrder.getShippingFee()).add(goodsOrder.getInsureFee()).add(goodsOrder.getPayFee()).subtract(goodsOrder.getDiscount());
		goodsOrder.setOrderAmount(orderAmount);
		Date now = new Date();
		goodsOrder.setAddTime(now);
		goodsOrderService.saveGoodsOrder(goodsOrder);
		
		
		//保存订单商品信息.
		String sessionId = session.getId();
		List<CartGoods> list = cartGoodsService.findBySession(sessionId);
		if(list != null && list.size() >0){
			//购物车信息.
			for(CartGoods cg:list){
				OrderGoods og = new OrderGoods();
				og.setGoodsAmount(cg.getGoodsAmount());
				og.setGoodsName(cg.getGoodsName());
				og.setGoodsNo(cg.getGoodsNo());
				og.setGoodsPrice(cg.getGoodsPrice());
				
				og.setOrderNo(goodsOrder.getOrderSn());
				orderGoodsService.saveOrderGoods(og);
			}
		}
		
		//删除session购物车对象.
		for(CartGoods cg:list){
			System.out.println("delete cartgoods...");
			cartGoodsService.removeCartGoods(cg.getCartId());
		}
		
		return "redirect:/goods/order/detail?orderNo="+goodsOrder.getOrderSn();
	}
	
	/**
	 * 
	 * @return
	 */
	@RequestMapping("/goods/order/detail")
	public String detail(String orderNo,ModelMap model){
		//订单信息.
		GoodsOrderInfo goodsOrder = goodsOrderService.getGoodsOrder(orderNo);
		model.addAttribute("order", goodsOrder);
		
		//订单商品.
		
		return "front/goodsorder/detail";
	}

}
